home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / nasanets.zip / SYSDEP.C < prev    next >
C/C++ Source or Header  |  1990-06-07  |  11KB  |  392 lines

  1. /*=============================*/
  2. /*           NETS              */
  3. /*                             */
  4. /* a product of the AI Section */
  5. /* NASA, Johnson Space Center  */
  6. /*                             */
  7. /* principal author:           */
  8. /*       Paul Baffes           */
  9. /*                             */
  10. /* contributing authors:       */
  11. /*      Bryan Dulock           */
  12. /*      Chris Ortiz            */
  13. /*=============================*/
  14.  
  15.  
  16. /*
  17. ----------------------------------------------------------------------
  18.   All System-Dependent Code Resides Here (Prefix = sys_)
  19. ----------------------------------------------------------------------
  20.   This code is divided into 4 major sections:
  21.  
  22.   (1) include files
  23.   (2) subroutines
  24.  
  25.   Each section is further explained below.
  26. ----------------------------------------------------------------------
  27. */
  28.  
  29.  
  30. /*
  31. ----------------------------------------------------------------------
  32.   INCLUDES AND EXTERNS
  33. ----------------------------------------------------------------------
  34. */
  35. #include     "common.h"
  36. #include     "weights.h"
  37. #include     "layer.h"
  38. #include     "net.h"
  39. #include     "netio.h"
  40. extern void  IO_print();
  41. extern char  IO_str[MAX_LINE_SIZE];
  42.  
  43.  
  44. /*
  45. ======================================================================
  46.   ROUTINES IN SYSDEP.C                                                   
  47. ======================================================================
  48.   The routines in this file are grouped below by function.  Each routine
  49.   is prefixed by the string "sys_" indicating that it is defined in the 
  50.   "sysdep.c" file.  The types returned by the routines are also shown here
  51.   so that cross checking is more easily done between these functions
  52.   and the other files which intern them.
  53.  
  54.  
  55.   Type Returned                 Routine                                 
  56.   -------------                 -------                                 
  57.     char *                      sys_long_alloc
  58.     char *                      sys_alloc
  59.     void                        sys_long_free
  60.     void                        sys_free
  61.     float                       sys_get_time
  62.     void                        sys_init_rand
  63.     void                        sys_delete_file
  64.     void                        sys_rename_file
  65. ======================================================================
  66. */
  67.  
  68.  
  69. char  *sys_long_alloc(size)
  70. long  size;
  71. /*
  72. ----------------------------------------------------------------------
  73.   Although not really system dependent code, the get_mem routine was
  74.   placed here because its activities are usually associated with system
  75.   calls. All this routine does is take the place of "malloc" so that
  76.   I can check for lack of space and handle the error gracefully, ie,
  77.   by printing out a message before quiting the program.
  78.   
  79.   The trick here is to do the right kind of malloc. Normally, the 
  80.   typical "malloc" function which comes with the compiler takes an
  81.   unsigned integer as an argument. Thus you cannot pass it a long 
  82.   argument. 
  83. ----------------------------------------------------------------------
  84. */
  85. BEGIN
  86.    char          *r, *sys_check_alloc();
  87.    unsigned int  test;
  88.    
  89.    /*-----------------------------------------*/
  90.    /* allocate the memory in chars, using the */
  91.    /* proper routine depending on compiler    */
  92.    /*-----------------------------------------*/
  93.    if (size > 0)
  94. #if LSPEED
  95.       r = mlalloc(size);
  96. #endif  
  97.  
  98. #if IBM_TBC
  99.       r = farmalloc(size);
  100. #endif
  101.  
  102. #if IBM_MSC
  103.       r = halloc(size,1);
  104. #endif
  105.   
  106. #if (! LSPEED) && (! IBM_TBC) && (! IBM_MSC)
  107.    BEGIN
  108.       test = (unsigned int) size;
  109.       if (test != size)
  110.          r= NULL;
  111.       else r = (char *) malloc(test);
  112.    ENDIF
  113. #endif
  114.  
  115.    else 
  116.       r = NULL;
  117.    
  118.    /*---------------------------------------------*/
  119.    /* then check to see that the memory was there */
  120.    /*---------------------------------------------*/
  121.    return(sys_check_alloc(r));
  122.       
  123. END /* sys_long_alloc */
  124.  
  125.  
  126. char  *sys_alloc(size)
  127. unsigned int  size;
  128. /*
  129. ----------------------------------------------------------------------
  130.   This routine is called for memory allocation when the caller KNOWS
  131.   that a large memory allocation is not necessary.
  132. ----------------------------------------------------------------------
  133. */
  134. BEGIN
  135.    char  *r, *sys_check_alloc();
  136.  
  137.    if (size > 0)
  138.       r = (char *) malloc(size);
  139.    else 
  140.       r = NULL;
  141.    return(sys_check_alloc(r));
  142.       
  143. END /* sys_alloc */
  144.  
  145.  
  146. char  *sys_check_alloc(ptr_mem)
  147. char  *ptr_mem;
  148. /*
  149. ----------------------------------------------------------------------
  150.   Just a subroutine called by the allocation routines above to check if
  151.   the pointer to memory is NULL. If so, then out of memory, thus exit
  152.   the program.
  153. ----------------------------------------------------------------------
  154. */
  155. BEGIN
  156.    /*---------------------------------------*/
  157.    /* if the result is NULL exit with error */
  158.    /*---------------------------------------*/
  159.    if (ptr_mem == NULL) BEGIN
  160.       sprintf(IO_str, "\n*** out of memory ***\n");
  161.       IO_print(0);
  162.       exit(1);
  163.    ENDIF
  164.    return(ptr_mem);
  165.       
  166. END /* sys_check_alloc */
  167.  
  168.   
  169. void  sys_long_free(ptr)
  170. char *ptr;
  171. /*
  172. ----------------------------------------------------------------------
  173.   This free routine is the sister of the sys_get_mem routine. It calls
  174.   the proper routine for freeing the memory, depending upon the compiler
  175.   used. 
  176. ----------------------------------------------------------------------
  177. */
  178. BEGIN
  179.  
  180. #if LSPEED
  181.    free(ptr);
  182. #endif  
  183.   
  184. #if IBM_TBC
  185.    farfree(ptr);
  186. #endif
  187.  
  188. #if IBM_MSC
  189.    hfree(ptr);
  190. #endif
  191.  
  192. #if (! LSPEED) && (! IBM_TBC) && (! IBM_MSC)
  193.    free(ptr);
  194. #endif
  195.  
  196. END /* sys_long_free */
  197.  
  198.  
  199. void  sys_free(ptr)
  200. char  *ptr;
  201. /*
  202. ----------------------------------------------------------------------
  203.   Just to keep everything parallel, I wrote this "normal case" free 
  204.   routine which is the sister to the sys_alloc routine above. All this 
  205.   does is call the generic free routine of the system.
  206. ----------------------------------------------------------------------
  207. */
  208. BEGIN
  209.    free(ptr);
  210.    
  211. END /* sys_free */
  212.  
  213.  
  214. float  sys_get_time()                          
  215. /*
  216. ----------------------------------------------------------------------
  217.  float sys_get_time()                                                     
  218.                                                                       
  219.  Returns a floating point value of the current time.  Taken from      
  220.   the CLIPS code, which is written in a more portable form there.     
  221.  Note that there are several '#if' statements here.  These look for   
  222.   values defined within 'common.h' to see whether or not they should  
  223.   compile.  That is, if "VMS" is defined to 1 then any code           
  224.   surrounded by a '#if VMS' statement will be compiled.  This is a    
  225.   simple way to make code portable.                                   
  226. ----------------------------------------------------------------------
  227. */
  228. BEGIN
  229. #if   VMS || IBM_MSC || UNIX_BER
  230.    float sec, msec, time;
  231.    long   temp;
  232.    struct timeb time_pointer;
  233.  
  234.    ftime(&time_pointer);
  235.    temp = time_pointer.time;
  236.    temp = temp - ((temp/10000) * 10000);
  237.    sec  = (float) temp;
  238.    msec = (float) time_pointer.millitm;
  239.    return(sec + (msec / 1000.0));
  240. #endif
  241.  
  242. #if   UNIX_ATT 
  243.    long t_int;
  244.    float t;
  245.    struct tms buf;
  246.  
  247.    t_int = times(&buf);
  248.    t = (float) t_int / 60.0;
  249.    return(t);
  250. #endif
  251.  
  252. #if   IBM_TBC
  253.    struct time  now;
  254.    float        t;
  255.    
  256.    gettime(&now);
  257.    t = ((float)now.ti_hour * 3600.0)
  258.        + ((float)now.ti_min * 60.0)
  259.        + (float)now.ti_sec;
  260.    return(t);
  261. #endif
  262.  
  263. #if   LSPEED
  264.    struct tm  *now;
  265.    float        t;
  266.    
  267.    now = localtime(0L);
  268.    t = ((float)now->tm_hour * 3600.0)
  269.        + ((float)now->tm_min * 60.0)
  270.        + (float)now->tm_sec;
  271.    return(t);
  272. #endif
  273.  
  274. #if  WIZARD
  275.    time_t  the_time;
  276.    
  277.    return( (float)time(&the_time) );
  278. #endif
  279.  
  280. END /* sys_get_time */
  281.  
  282.  
  283. void  sys_init_rand()
  284. /*
  285. ----------------------------------------------------------------------
  286.   SYS_INIT_RAND will initialize the random number generator
  287.                  
  288.   Author : Chris Ortiz ( MPAD/AI NASA )
  289.   Date   : 2-Jun-1989
  290.   Cyclomatic Complexity = 1
  291.  
  292.   Variables Used
  293.   --------------
  294.      tm         - Structure Time is stored in.
  295.      seed       - Random Number generator seed.
  296.      
  297.   Procedures / Functions
  298.   ----------------------
  299.      Srand     - Initialize Random number generator.
  300.      Localtime - Gets Local Time
  301.  
  302.   Algorithm
  303.   ---------
  304.      1. Get current time.
  305.      2. Multiply minutes by the seconds to come up with a seed.
  306.      3. Set random number generator with seed.
  307. ----------------------------------------------------------------------
  308. */
  309. BEGIN
  310. #if   LSPEED
  311.    struct tm  *time;
  312.    float      seed;
  313.    
  314.    time = localtime(0L);
  315.    seed = ((float)time->tm_min * (float)time->tm_sec);
  316.    srand ((int)seed);
  317. #endif
  318.  
  319. #if   IBM_TBC
  320.    srand( (long)sys_get_time() );
  321. #endif
  322.  
  323. #if   VMS || IBM_MSC || UNIX_BER
  324.    struct timeb time_pointer;
  325.  
  326.    ftime(&time_pointer);
  327.    srand( (unsigned)time_pointer.time );
  328. #endif
  329.  
  330. #if   UNIX_ATT 
  331.    long t_int;
  332.    struct tms buf;
  333.  
  334.    t_int = times(&buf);
  335.    srand( (unsigned)t_int );
  336. #endif
  337.  
  338. #if  WIZARD
  339.    time_t  t_int;
  340.    
  341.    srand( (unsigned int)time(&t_int) );
  342. #endif
  343.  
  344. END /* sys_init_rand */
  345.  
  346.  
  347. void  sys_delete_file(filename)
  348. char  *filename;
  349. /*
  350. ----------------------------------------------------------------------
  351.   Deletes the file with the name "filename" from the system. For now,
  352.   it only deletes from the same directory.
  353. ----------------------------------------------------------------------
  354. */
  355. BEGIN
  356. #if  LSPEED || VMS || IBM_MSC || IBM_TBC || WIZARD
  357.    remove(filename);
  358. #endif
  359.  
  360. #if  UNIX_ATT || UNIX_BER
  361.    unlink(filename);
  362. #endif
  363.  
  364. END /* sys_delete_file */
  365.  
  366.  
  367. void  sys_rename_file(oldname, newname)
  368. char  *oldname, *newname;
  369. /*
  370. ----------------------------------------------------------------------
  371.   Renames the file with the name "oldname" to the new name given in 
  372.   the second parameter.
  373. ----------------------------------------------------------------------
  374. */
  375. BEGIN
  376. #if  !UNIX_ATT
  377.    rename(oldname, newname);
  378. #endif
  379.  
  380. #if  UNIX_ATT
  381.    char  command[80];
  382.    
  383.    /*------------------------------------------*/
  384.    /* for AT&T Unix, make a system call to get */
  385.    /* the unix command "mv" to do the renaming */
  386.    /*------------------------------------------*/
  387.    sprintf(command, "mv %s %s", oldname, newname);
  388.    system(command);
  389. #endif
  390.  
  391. END /* sys_rename_file */
  392.